package com.yangguang.huahui.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Environment;
import android.util.Log;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImgDispose {

	
	/**
	 * 图片压缩
	 * 
	 * @param oldWidth
	 * @param oldHeight
	 * @param newWidth
	 * @param newHeight
	 * @return
	 */
	public static int reckonThumbnail(int oldWidth, int oldHeight, int newWidth, int newHeight) {
		if ((oldHeight > newHeight && oldWidth > newWidth) || (oldHeight <= newHeight && oldWidth > newWidth)) {
			int be = (int) (oldWidth / (float) newWidth);
			if (be <= 1)
				be = 1;
			return be;
		} else if (oldHeight > newHeight && oldWidth <= newWidth) {
			int be = (int) (oldHeight / (float) newHeight);
			if (be <= 1)
				be = 1;
			return be;
		}
		return 1;
	}

	/**
	 * 图片缩放
	 * 
	 * @param bmp
	 * @param width
	 * @param height
	 * @return
	 */
	public static Bitmap PicZoom(Bitmap bmp, int width, int height) {
		int bmpWidth = bmp.getWidth();
		int bmpHeght = bmp.getHeight();
		Matrix matrix = new Matrix();
		matrix.postScale((float) width / bmpWidth, (float) height / bmpHeght);
		return Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeght, matrix, true);
	}

	/**
	 * 质量压缩方法
	 *
	 * @param image
	 * @return
	 */
	public static Bitmap compressImage(Bitmap image) {

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		image.compress(Bitmap.CompressFormat.PNG, 100, baos);// 质量压缩方法，这里100表示不压缩，把压缩后的数据存放到baos中
		int options = 90;

		while (baos.toByteArray().length / 1024 > 100) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
			baos.reset(); // 重置baos即清空baos
			image.compress(Bitmap.CompressFormat.PNG, options, baos);// 这里压缩options%，把压缩后的数据存放到baos中
			options -= 10;// 每次都减少10
		}
		ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
		Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片
		return bitmap;
	}

	/**
	 * 保存
	 * 
	 * @param bm
	 * @return file
	 */
	public static File SaveBitmap(Bitmap bm, String filePaht) {
//		File img = new File(getSDPath(),"huahui.jpg");
		File img = new File(filePaht);
		Log.e("path=====",  img + "");
		FileOutputStream fos=null;

		try {
			fos = new FileOutputStream(img);
			bm.compress(Bitmap.CompressFormat.JPEG, 90, fos);
			// return Uri.fromFile(img);
		} catch (Exception e) {
			Log.e("#################3",e.getMessage());
			return null;
		}finally{
			if(fos!=null){
				try {
					fos.flush();
					fos.close();
				} catch (IOException e) {
					Log.e("#################4",e.getMessage());
				}
			}
		}
		return img;
	}

	public static String getSDPath(){
		String sdDir = "";
		try {
			boolean sdCardExist = Environment.getExternalStorageState().equals(
					Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
			if (sdCardExist) {
				sdDir = Environment.getExternalStorageDirectory() + "/huahui/";// 获取跟目录
			} else {
				sdDir = "/data/data/com.yangguang.huahui/huahui/";
			}
		}catch (Exception e){
		}
		try {
			File file = new File(sdDir);
			if (!file.exists()) {
				file.mkdir();
			}
		}catch(Exception e){
		}

		return sdDir;
	}
	
}
